#pragma warning(disable:6031)
#include <shlobj.h>
#include"elib/fnshare.h"
namespace elibshell {
	char* GetShortCutTarget(const std::string_view& lpszLinkFilePath)
	{
		SHFILEINFOA info = { 0 };
		char* ret = nullptr;
		if (SHGetFileInfoA(lpszLinkFilePath.data(), 0, &info, sizeof(info), SHGFI_ATTRIBUTES) != 0 && (info.dwAttributes & SFGAO_LINK) != 0)
		{
			IShellLinkA* piShellLink;
			if (SUCCEEDED(CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLinkA, (void**)&piShellLink)))
			{
				IPersistFile* piPersistFile;
				if (SUCCEEDED(piShellLink->QueryInterface(IID_IPersistFile, (void**)&piPersistFile))) {
					// Convert the string to a wide string
					int len = MultiByteToWideChar(CP_ACP, 0, lpszLinkFilePath.data(), -1, NULL, 0);
					std::wstring wsLinkFilePath(len, 0);
					MultiByteToWideChar(CP_ACP, 0, lpszLinkFilePath.data(), -1, &wsLinkFilePath[0], len);
					if (SUCCEEDED(piPersistFile->Load((LPCOLESTR)wsLinkFilePath.c_str(), STGM_READ)))
					{
						CHAR path[MAX_PATH];
						path[0] = '\0';
						if (SUCCEEDED(piShellLink->GetPath(path, MAX_PATH, NULL, 0)))
							ret = clone_text(path);
					}
					piPersistFile->Release();
				}
				piShellLink->Release();
			}
		}
		return ret;
	}
}